// GaojieLog.cpp : Defines the exported functions for the DLL.
//

#include "pch.h"
#include "GaojieLog.h"
#include "GaojieLogApp.h"
#include "..\Common\StrHelper.h"

#define LEVEL2CHAR(level)	"NEWI"[(int)level]

CGaojieLogApp g_theApp;

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
	switch (dwReason)
	{
	case DLL_PROCESS_ATTACH:
		break;

	case DLL_PROCESS_DETACH:
		break;
	}

	return TRUE;
}

namespace Log
{

bool LogInit()
{
	AssertInUIThread();

	return g_theApp.Init(_ReturnAddress());
}

void LogExit()
{
	AssertInUIThread();

	return g_theApp.Exit(_ReturnAddress());
}

bool LogWriteA(LPCVOID lpRetAddr, LogLevel logLevel, LPCSTR lpFormat, ...)
{
	if (!g_theApp.IsLoggable(logLevel))
		return false;

	if (!lpFormat)
		return true;

#ifndef _DEBUG
	lpRetAddr = _ReturnAddress();
#endif

	std::string strLine;
	strLine += LEVEL2CHAR(logLevel);
	strLine += ' ';

	va_list args;
	va_start(args, lpFormat);
	Str::AppendFormatV(strLine, lpFormat, args);
	va_end(args);
	g_theApp.WriteLine(lpRetAddr, strLine);
	return true;
}

bool LogWriteW(LPCVOID lpRetAddr, LogLevel logLevel, LPCWSTR lpFormat, ...)
{
	if (!g_theApp.IsLoggable(logLevel))
		return false;

	if (!lpFormat)
		return true;

#ifndef _DEBUG
	lpRetAddr = _ReturnAddress();
#endif

	std::wstring strLine;
	strLine += LEVEL2CHAR(logLevel);
	strLine += ' ';

	va_list args;
	va_start(args, lpFormat);
	Str::AppendFormatV(strLine, lpFormat, args);
	va_end(args);
	g_theApp.WriteLine(lpRetAddr, Str::Unicode2ANSI(strLine));
	return true;
}

}	// namespace Log
